package com.credit.jumu.calculator.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.base.BaseActivity;
import com.credit.jumu.calculator.utils.LogUtils;

/**
 * Created by Administrator on 2017/10/27/027.
 */

public class MyWebActivity extends BaseActivity {
	WebView webView;
	String url;
	ImageView back;
	TextView tv_title;
	private ProgressBar mypr;
	ImageView refresh;
	TextView barheight;
	String myTitle = "";

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_web);
		barheight = (TextView) findViewById(R.id.barheight);
		RelativeLayout.LayoutParams Params1 = (RelativeLayout.LayoutParams) barheight.getLayoutParams();
		Params1.height = getNavigationBarHeight();
		barheight.setLayoutParams(Params1);
		toAllWindow();
		back = (ImageView) findViewById(R.id.back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				MyWebActivity.this.finish();
			}
		});
		webView = (WebView) findViewById(R.id.myallweb);
		Intent intent = getIntent();
		tv_title = (TextView) findViewById(R.id.tv_title);
		url = intent.getStringExtra("url");
		refresh = (ImageView) findViewById(R.id.web_refresh);
		LogUtils.println("zhangyiyi&&&&&" + url);
		if (TextUtils.isEmpty(url)) {
			return;
		}
		refresh.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				webView.reload();
			}
		});

		mypr = (ProgressBar) findViewById(R.id.webProgress);
		mypr.setMax(100);
		webSet();
	}

	private void webSet() {
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
		webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
		webSettings.setSupportZoom(true);
		webSettings.setDisplayZoomControls(false);
		webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
		webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
		webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能
		webSettings.setDatabaseEnabled(true);   //开启 database storage API 功能
		webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能.
		webView.loadUrl(url);
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				handler.proceed();    //表示等待证书响应
				// handler.cancel();      //表示挂起连接，为默认方式
				// handler.handleMessage(null);    //可做其他处理
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				mypr.setVisibility(View.VISIBLE);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				mypr.setVisibility(View.GONE);
				tv_title.setText(myTitle);
			}
		});
		webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				mypr.setProgress(newProgress);
			}

			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				myTitle = title;
			}
		});
	}
}
